<template>
  <view class="ver-layout" style="padding: 10px">
    <view>Note(optional):</view>
    <u--textarea class="input-view" v-model="formData.inputValue"
                 :placeholder="placeHolder"></u--textarea>
    <u-upload class="upload-layout" :fileList="formData.image" @afterRead="afterRead" name="1" multiple
              :maxCount="10" uploadIconColor="#999999"></u-upload>
    <view v-for="(item,index) in hintList" :key="index" style="font-weight: lighter;">
      <view v-html="item" style="font-size: small;line-height: 23px"></view>
    </view>
    <view>
      <u-button type="primary" text="提交" style="margin-top: 50px" @click="submit">提交</u-button>
    </view>
  </view>
</template>
<script>
export default {
  name: "sellCard",
  data() {
    return {
      placeHolder: "you can note card info here or just leave it blank",
      formData: {
        inputValue: '',
        image: []
      },
      hintList: [
        '1. Please upload <text style="color: #4fd31c">clear card picture</text>',
        '2. If your gift card is Amazon, you will get a better  exchange rate with a receipt.',
        '<text style="color: #4fd31c">3. Once you upload your card picture, We will deal with it.</text>',
        '4. Most cards will be processed in <text style="color: #4fd31c">2-10 minutes</text>, some 4. Most cards will be processed in <text style="color: #4fd31c">2-10 minutes</text>, some',
        '<text style="color: #4fd31c">5. Vanilla and visa gift cards, need to upload the front of the card and the back of the card.</text>',
        '<text style="color: #4fd31c">6. Vanilla/visa gift cards has different rate by different first four digits.</text>',
        '7. If you want to trade Cash App,please chat with us.',
      ],
      renderData: {}
    }
  },
  methods: {
    receiveRenderData(data) {
      this.renderData = data
    },
    // 删除图片
    deletePic(event) {
      this[`fileList${event.name}`].splice(event.index, 1)
    },
    // 新增图片
    async afterRead(event) {
      let addList = event.file.map(item => {
        return {...item, status: 'uploading', message: '上传中'}
      })
      this.formData.image = this.formData.image.concat(addList)
      this.uploadPromise()
    },
    uploadPromise() {
      setTimeout(() => {
        this.formData.image = this.formData.image.map(item => {
          return {
            ...item,
            status: 'success',
            message: '',
            // url: result
          }
        })
      }, 1000)
    },
    submit() {
      const sendTime = new Date().getTime()
      let chat = {fromUserName: '18810126510', text: '测试', sessionId: '18810126510' + new Date().getTime()}
      let data = {
        'robotCode': 'hn2twk',
        'channelCode': 'A',
        'sessQaType': 'QA',
        'fromUserName': chat.fromUserName,
        'questions': chat.text,
        'sendTime': sendTime,
        'sessionId': chat.sessionId
      }
      uni.request({
        url: 'http://yxchat.impc.com.cn/robot/sendMsgToRobot',
        method: 'POST',
        header: {'Content-Type': 'application/json'},
        data: data,
        success: res => {
          console.log("request==", res);
        },
        fail: (err) => {
          console.error("request==", err);
        },
        complete: () => {
        }
      })
    }
  }
}
</script>
<script module="render" lang="renderjs">
export default {
  data() {
    return {
      messageData: {name: '222', age: 333}
    }
  },
  mounted() {
    this.connectSee()
  },
  methods: {
    connectSee() {
      console.log("mounted======")
      if (window.EventSource) {
        let chat = {sex: '1', nickName: '18810126510', fromUserName: '11'}
        let url = `http://yxchat.impc.com.cn/vuechat/getConnection?channel=A&sex=${chat.sex}&nickName=${chat.nickName}&fromUserName=${chat.fromUserName}`
        const se = new EventSource(url)
        console.log('EventSource', se)
        console.log('EventSource', url)
        se.addEventListener('open', function (e) {
          console.log('EventSource', '连接打开')
        })
        se.addEventListener('message', function (e) {
          console.log("EventSource,message", e)
        })
        se.addEventListener('error', function (e) {
          console.log('连接断掉了')
          se.close();
        })
      }
    },
    setUniData(event, ownerInstance) {
      console.log("setUniData",this.messageData)
      //this.messageData = {name: '222', age: 333}
      //this.$ownerInstance.callMethod('receiveRenderData', this.messageData)
      ownerInstance.callMethod('receiveRenderData', this.messageData)
    }
  }
}

</script>
<style scoped>
page {
  background: white;
}

.input-view {
  margin-top: 10px;
}

.upload-layout {
  margin-top: 10px;
}
</style>
